package com.cino.demos.controller.shortlink;


import com.cino.demos.common.aspect.annotation.AutoLog;
import com.cino.demos.service.ShortLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/shortlink")
public class ShortLinkContorller {

    @Autowired
    private ShortLinkService shortLinkService;

    @RequestMapping("/getString")
    @AutoLog
    public String getString(){
        return "hello,itishere";
    }

    @AutoLog
    @RequestMapping("/re")
    public String redirectDemo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.sendRedirect("/getString");
        System.out.println("到这里了么？");
        return "done?";
    }

    /**
     * http://localhost:8080/shortlink/redirect
     */
    @GetMapping("/redirect/{id}")
    public String ShortLinkRedirect(HttpServletRequest request,HttpServletResponse response,@PathVariable("id") Integer id) throws IOException {
        shortLinkService.shortLinkJump(request,response,id);
        return "redirect,it is Okay";
    }

    /**
     * http://localhost:8080/shortlink/insert
     * @return
     */
    @PostMapping("/insert")
    public String ShortLinkInsert() {
        shortLinkService.insert();
        return "itisOkay";
    }
}
